package chpt08;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class BufferedStreamTest {

	@org.junit.Test
	public void PrintWritertest() {
		PrintWriter pw = null;
		try {
			pw = new PrintWriter("test.txt");
			for (int i = 0; i < 10000000; i++) {
				pw.println(1);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			pw.close();
		}
	}

	@org.junit.Test
	public void BufferedReadertest() {
		FileReader fr = null;
		BufferedReader br = null;
		try {
			fr = new FileReader("test.txt");
			br = new BufferedReader(fr);
			@SuppressWarnings("unused")
			String line = null;
			while ((line = br.readLine()) != null)
				;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (br != null)
					br.close();
				fr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@org.junit.Test
	public void ScannerReadertest() {
		Scanner sc = null;
		try {
			sc = new Scanner(new File("test.txt"));
			while (sc.hasNextLine()) {
				sc.nextLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			sc.close();
		}
	}

	@org.junit.Test
	public void BufferedWritertest() {
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter("test1.txt"));
			for (int i = 0; i < 10000000; i++) {
				bw.write(1);
				bw.newLine();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				bw.flush();
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
